<template>
  <div>
    <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
    <div style="display: flex; justify-content: space-between">
      <div class="right">
        <el-form
          :inline="true"
          ref="searchForm"
          :model="search"
          class="demo-form-inline"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="search.name" placeholder="门店地址"></el-input>
          </el-form-item>
          <el-form-item label="编码" prop="id">
            <el-input v-model="search.id" placeholder="业态"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div class="left">
        <el-link type="primary" @click="resetForm('searchForm')"
          >重置筛选条件</el-link
        >
        <el-button
          type="primary"
          @click="
            search.current = 1;
            getArea();
          "
          icon="el-icon-search"
          >查询</el-button
        >
      </div>
    </div>

    <el-table
      :header-cell-style="{ background: '#eaedf4' }"
      :data="shop"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column type="index" width="50" label="序号"> </el-table-column>
      <el-table-column prop="id" label="编码"> </el-table-column>
      <el-table-column prop="name" label="名称"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit-outline"
            style="color: #4c93ff"
            @click="visibleEdit=true;check(scope.row);"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            style="color: #ff8e8d"
            @click="delArea(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加区域 -->
    <el-dialog title="增加区域" :visible.sync="dialogFormVisible">
      <el-form :model="search">
        <el-form-item label="编码" prop="id">
          <el-input v-model="search.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="search.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addArea()">确 定</el-button>
      </div>
    </el-dialog>

    <!--  编辑区域-->
    <EditArea :visible.sync="visibleEdit" @editSuccess="getArea()" :id="id" ></EditArea>
  </div>
</template>
<script>
import { GetArea, AddArea, DelArea } from "@/api/shop";
import EditArea from "@/components/EditArea.vue";
export default {
  components:{
    EditArea
  },
  data() {
    return {
      shop: [],
      ta1: '',
      ta2:'',
      search: {},
      dialogFormVisible: false,
      visibleEdit:false,
      id:{}

    };
  },
  created() {
    
    this.getArea();
  },
  methods: {
    check(e) {
      console.log(e);
      this.id = e;
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.getArea();
    },
    delArea(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DelArea(id).then((res) => {
            if (res && res.data && res.data.code === 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              // 删除成功，重新获取员工
              this.getArea();
            } else {
              this.$message({
                type: "success",
                message: res.data.msg || "删除失败",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addArea() {
      AddArea(this.search).then((res) => {
        if (res && res.data && res.data.code === 0) {
          console.log(res);
          this.search = res.data.data;
          this.$message({
            type: "success",
            message: "新增成功，2秒后自动返回",
          });
          setTimeout(() => {
            // this.$router.push("/admin/shop/areaList");
            this.getArea();
          }, 1500);
        }
        // 关闭弹框
        this.dialogFormVisible = false;
      });
    },
    getArea() {
      var search = {};
      // 如果是空的字段，不添加到搜索条件
      for (var k in this.search) {
        if (this.search[k]) {
          search[k] = this.search[k];
        }
      }
      GetArea(search).then((res) => {
        if (res.data.code === 0) {
          this.shop = res.data.data;
          console.log(this.shop, "区域信息");
          this.pagination = res.data.pagination;
          this.pagination.current = this.pagination.current * 1;
          this.pagination.size = this.pagination.size * 1;
        }
      });
    },
   
  },
};
</script>
